package com.cortex.pineapple.utilities;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Debug;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public class Utilities {
	public static void showKeyboard(boolean isOpen,View view){
		InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		// what action to perform on keyboard open/hide
		if(isOpen){
			imm.toggleSoftInputFromWindow(view.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
		}
		else{
			imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
		}
	}
	
	public static List<String> csv2ArrayList(String str){
		List<String> ret = new ArrayList<String>();
		if(str.isEmpty()){
			return ret;
		}
		String[] stringArr = str.split(",");
		for (int i = 0; i < stringArr.length; i++) {
			ret.add(stringArr[i]);
		}
		return ret;
	}
	public static String list2csv(List<String> list){
		StringBuilder ret = new StringBuilder();
		if(list.isEmpty()){
			return ret.toString();
		}
		for (int i = 0; i < list.size()-1; i++) {
			ret.append(list.get(i));
			if(i < list.size()){
				ret.append(",");
				break;
			}
		}
		
		return ret.toString();
	}
	public static List<String> csv2ArrayListImprove(String str){
		List<String> ret = new ArrayList<String>();
		if(str.isEmpty()){
			return ret;
		}
		String[] stringArr = str.split(",:,");
		for (int i = 0; i < stringArr.length; i++) {
			ret.add(stringArr[i]);
		}
		return ret;
	}
	public static String list2csvImprove(List<String> list){
		StringBuilder ret = new StringBuilder();
		if(list.isEmpty()){
			return ret.toString();
		}
		for (int i = 0; i < list.size(); i++) {
			ret.append(list.get(i));
			if(i < list.size()){
				ret.append(",:,");
			}
		}
		
		return ret.toString();
	}
	public static int boolean2integer(boolean value){
		return value?1:0;
	}
	public static boolean integer2boolean(int value){
		return value == 1?true:false;
	}
	public static void logHeap() {
        Double allocated = new Double(Debug.getNativeHeapAllocatedSize())/new Double((1048576));
        Double available = new Double(Debug.getNativeHeapSize())/1048576.0;
        Double free = new Double(Debug.getNativeHeapFreeSize())/1048576.0;
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);

        Log.d(MessagesLog.APPLICATION, "debug. =================================");
        Log.d(MessagesLog.APPLICATION, "debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available) + "MB (" + df.format(free) + "MB free)");
        Log.d(MessagesLog.APPLICATION, "debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory()/1048576)) + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory()/1048576))+ "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory()/1048576)) +"MB free)");
    }
}
